package hibu.LeiJonGun.seniorservice;
import hibu.LeiJonGun.activities.ActAssignmentArticle;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

//Created by Elis Tor Jonsson

public class DialogArticle extends DialogFragment implements OnClickListener{
	
    private TextView txtNumber;
    private Button btnOK;
    private Button btnPlus;
    private Button btnMinus;
    int numberArticles = 1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

    	getDialog().setTitle("Antall:");
        View view = inflater.inflate(R.layout.dialog_article, container);
        txtNumber = (TextView) view.findViewById(R.id.txtDialogNumber);
        txtNumber.setText(""+numberArticles);
        btnOK = (Button) view.findViewById(R.id.btnDialogOK);
        btnPlus = (Button) view.findViewById(R.id.btnDialogPlus);
        btnMinus = (Button) view.findViewById(R.id.btnDialogMinus);
        btnMinus.setOnClickListener(this);
        btnPlus.setOnClickListener(this);
        btnOK.setOnClickListener(this);
        return view;
    }
    

	public void onClick(View v) {
		ActAssignmentArticle actassignmentarticle = (ActAssignmentArticle) getActivity();
		String selected = v.getTag().toString();
		
		if (selected.equals("ok")){
		    	try {
		    		actassignmentarticle.addArticleNumberToList(numberArticles);
		    	} catch (Exception e) {
				e.printStackTrace();}
			this.dismiss();
		}
		if (selected.equals("plus")){
			numberArticles+=1;
			txtNumber.setText("" + numberArticles);
		}
		if (selected.equals("minus")){
			numberArticles-=1;
			if (numberArticles < 0) numberArticles = 0;
			txtNumber.setText("" + numberArticles);
		}
		
	}
	
	//Is set in ActAssignmentArticle when an article already is chosen
    public void setNumber(int numberArticles){
    	this.numberArticles = numberArticles;
    }
}

